<?php

	class My_Custom_Functions
	{
  		public function __construct()
		{
			//$_trCharsLower = array("ı", "i", "ü", "ş", "ç", "ö", "ğ");
	  		//$_trCharsUpper = array("I", "İ", "Ü", "Ş", "Ç", "Ö", "Ğ");

			$this->trCharsLower = array("ı", "i", "ü", "ş", "ç", "ö", "ğ");
			$this->trCharsUpper = array("I", "İ", "Ü", "Ş", "Ç", "Ö", "Ğ");

			$this->trcharsTitle = array("ı", "ü", "ş", "ç", "ö", "ğ", "İ", "Ü", "Ş", "Ç", "Ö", "Ğ");
			$this->encharsTitle = array("i", "u", "s", "c", "o", "g", "i", "u", "s", "c", "o", "g");

		}

		function generatePermalink($string, $id=null) {

			$string = preg_replace("/[^A-Za-z0-9\s\s+]/", '', $string);
			$string = str_replace($this->trcharsTitle, $this->encharsTitle, $string);
			$string = mb_strtolower($string);
			if ($id) {
				$string .= '-'.$id;
			}

			return $string;
		}

		public function strtoupperTurkish($string)
		{
			$string = trim($string);
			$string = $this->removeDuplicateSpaces($string);
			$string = str_replace($this->trCharsLower, $this->trCharsUpper, $string);
			$string = mb_strtoupper($string);

			return $string;

		}

		public function filterHtmlSpecialChars($string) {

			if (!$string) {
				return $string;
			}
			return htmlspecialchars(stripslashes(trim($string)), ENT_QUOTES);

		}

		public function ucfirstTurkish($string)
		{

			$string = trim($string);
			$string = str_replace('.', ' ', $string);
			$string = $this->removeDuplicateSpaces($string);
			$stringArr = array_filter(explode(' ', $string));
			if (empty($stringArr)) {
				return $string;
			}

			foreach ($stringArr as $value) {
				$value = trim($value);

				$value = str_replace($this->trCharsUpper, $this->trCharsLower, $value);
				$value = mb_strtolower($value);

				if (mb_strlen($value) > 1) {
					$firstChar = mb_substr($value, 0, 1);
					$firstChar = str_replace($this->trCharsLower, $this->trCharsUpper, $firstChar);
					$firstChar = mb_strtoupper($firstChar);
					$lastPart  = mb_substr($value, 1);
					$newStringArr[] = $firstChar . $lastPart;

				} else {
					$firstChar = mb_substr($value, 0, 1);
					$firstChar = str_replace($this->trCharsLower, $this->trCharsUpper, $firstChar);
					$firstChar = mb_strtoupper($firstChar);
					$newStringArr[] = $firstChar . '.';

				}

			}
			if (empty($newStringArr)) {
				return $string;
			}
			$newString = implode(' ', $newStringArr);
			return $newString;
		}

		public function removeDuplicateSpaces ($text) {

			$find = "/\s+/";
			$replace = " ";
			$newText = preg_replace($find, $replace, trim($text));
			return $newText;

		}

	    public function checkDateIsBetween($startDatetime, $endDatetime, $datetimeToCheckAgainst='')
	    {
	    	$mysqlDatetimeFormat = 'yyyy-MM-dd HH:mm:ss';

	    	$datetimeToCheckAgainst =  ( (!$datetimeToCheckAgainst) ? new Zend_Date() : $datetimeToCheckAgainst );


			if (empty($datetimeToCheckAgainst)) {
	    		return false;
	    		//throw new Exception('checking-semester-dates-current-datetime-is-empty');
			}

	    	if (Zend_Date::isDate($datetimeToCheckAgainst) == false) {
	    		return false;
	    		//throw new Exception('checking-semester-dates-current-datetime-is-not-valid');
	    	}



			if (empty($startDatetime)) {
	    		return false;
	    		//throw new Exception('checking-semester-dates-start-datetime-is-empty');
			}
			if (empty($endDatetime)) {
	    		return false;
	    		//throw new Exception('checking-semester-dates-end-datetime-is-empty');
			}
	    	if (!Zend_Date::isDate($startDatetime, $mysqlDatetimeFormat)) {
	    		return false;
	    		//throw new Exception('checking-semester-dates-start-datetime-is-not-valid: '.$startDatetime .'_'. $mysqlDatetimeFormat);
	    	}
	    	if (!Zend_Date::isDate($endDatetime, $mysqlDatetimeFormat)) {
	    		return false;
	    		//throw new Exception('checking-semester-dates-end-datetime-is-not-valid: '.$endDatetime .'_'. $mysqlDatetimeFormat);
	    	}

	        $startDatetime	= new Zend_Date($startDatetime, $mysqlDatetimeFormat);
	        $endDatetime	= new Zend_Date($endDatetime, 	$mysqlDatetimeFormat);



	        $datetimeToCheckAgainst	= $datetimeToCheckAgainst->toString(Zend_Date::TIMESTAMP);
	        $startDatetime		= $startDatetime->toString(Zend_Date::TIMESTAMP);
	        $endDatetime		= $endDatetime->toString(Zend_Date::TIMESTAMP);

	        //echo 'suan '.date('Y-m-d H:i:s', $datetimeToCheckAgainst).'<br />';
	        //echo 'basl '.date('Y-m-d H:i:s', $startDatetime).'<br />';
	        //echo 'bitis '.date('Y-m-d H:i:s', $endDatetime).'<br />';
	        //exit;
	        if ( ($datetimeToCheckAgainst > $startDatetime) && ($datetimeToCheckAgainst < $endDatetime) ) {
	        	return true;
	        }
	        return false;

	    }

        /* my ORIGINAL Translate function
         * public function translate($messageId, $locale = null)
        {
            $translateObj = Zend_Registry::get('translate');
            return $translateObj->translate($messageId, $locale);
        }*/
        public function translate($messageId, $params = array(), $locale = null)
        {
            $translateObj = Zend_Registry::get('Zend_Translate');
            $translateHelperObj = new Zend_View_Helper_Translate($translateObj);
            if ($locale) {
                return $translateHelperObj->translate($messageId, $params, $locale);
            }
            return $translateHelperObj->translate($messageId, $params);
        }

        public function _($messageId, $params = array(), $locale = null)
        {
            return $this->translate($messageId, $params, $locale);
        }

        public function specialTranslations($messageId, $params = array(), $locale = null)
        {//for poedit translate keys and files differentiation
            return $this->translate($messageId, $params, $locale);
        }

        public function testMyTranslate($messageId, $params = array(), $locale = null)
        {

            $translateObj = Zend_Registry::get('Zend_Translate');
            $translateHelperObj = new Zend_View_Helper_Translate($translateObj);
            if ($locale) {
                return $translateHelperObj->translate($messageId, $params, $locale);
            }
            return $translateHelperObj->translate($messageId, $params);

        }

	}

